package encaps.utils.rendering;

import encaps.resources.Response;
import encaps.utils.StringUtil;
import static encaps.utils.html.HTML.*;
import encaps.utils.html.HTMLElement;
import encaps.utils.html.HTMLElementSequence;
import encaps.utils.html.Styles;
import org.jdom.Element;

public class CommentSectionFactory implements FieldFactory {

    @Override
    public HTMLElement renderField(Element e, boolean edit) {
        String key = e.getChildTextTrim(Response.KEY);
        String value = e.getChildTextTrim(Response.VALUE);
        String label = e.getChildTextTrim(Response.LABEL);
        String message = e.getChildTextTrim(Response.MESSAGE);
        HTMLElement p = new HTMLElement(P);
        p.addContent(HTMLElement.createB(label + ": "));
        HTMLElement textControl = createTextControl(key, value);
        //System.out.println(textControl.toString());
        //textControl.addAttribute("name", key);
        //System.out.println(textControl.toString());
        p.addContent(textControl);
        p.addContent(renderMessage(message));
        return p;
    }

    protected HTMLElement createTextControl(String name, String value) {
        HTMLElementSequence result = new HTMLElementSequence();
        HTMLElement control = new HTMLElement(TEXTAREA);
        control.addAttribute(COLS, DEFAULT_COLS);
        control.addAttribute(ROWS, DEFAULT_ROWS);
        control.addAttribute(NAME, name);
//        WRAP=on

        if (value == null) {
            control.addContent("");
        } else {
            control.addContent(value);
        }
        result.addContent(HTMLElement.createBr());
        result.addContent(control);
        return result;
    }

    protected HTMLElement renderMessage(String message) {
        if (!StringUtil.isEmpty(message)) {
            HTMLElement span = new HTMLElement(SPAN);
            span.addAttribute(CLASS, Styles.VALIDATION_MESSAGE);
            span.addContent(" " + message);
            return span;
        } else {
            return null;
        }
    }
}
